﻿using System.Collections.Generic;
using System.Linq;
using Algoritmia.StateSpaces;
using Lists = Algoritmia.DataStructures.Lists;

namespace Algoritmia.Problems.GeneralizedCoinChange.ByScheme
{
    public class MoneyChangeBackwardsStateSpace : IBackwardsStateSpace<int,int>
    {
        private readonly int _Q;
        private readonly Lists.IList<int> _values;

        public MoneyChangeBackwardsStateSpace(int Q, Lists.IList<int> values)
        {
            _Q = Q;
            _values = values;
        }

        #region Implementation of IBackwardsStateSpace<int,int>

        public IEnumerable<int> FinalStates
        {
            get { yield return _Q; }
        }

        public IEnumerable<int> IncomingDecisions(int state)
        {
            return _values.Where(value => state - value >= 0);
        }

        public bool IsInitial(int state)
        {
            return state == 0;
        }

        public int Undo(int state, int decision)
        {
            return state - decision;
        }

        #endregion
    }
}
